package main

import (
	"fabric-explorer/controller"
	"fabric-explorer/listener"
	"fabric-explorer/service"
	"fabric-explorer/timer"
	"fabric-explorer/tools"
	"fabric-explorer/websocket"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

//初始化区块和交易数据
func initFabricData() {
	tools.InitDB()
	//clear database
	service.DeleteAllBlock()
	service.DeleteAllblockData()
	service.DeleteAllTransaction()
	service.DeleteAllNode()
	//service.DeleteAllChaincode()
	//init data
	//service.InitBlockData()
	//service.InitTransactionData(service.GetAllblockData())
	service.UpdateBlockData()
	service.InitNode()
	service.InitEvent()
	//service.InitChaincode()

	//listener用协程去跑
	go listener.BlockEventLinstener()
	go listener.ChannelEventLinstener()
	fmt.Println("-------------init data success-----------------")
	//go timer.AutoExec()
	fmt.Println("-------------AutoExec begin-----------------")
}

func main() {

	initFabricData()
	tools.InitDB()
	r := gin.Default()

	//加载html静态资源
	//r.Static("/static", "./static")
	r.Static("/static", "./static/dist")

	//加载html
	//r.LoadHTMLGlob("templates/*")
	// r.GET("/index", func(c *gin.Context) {
	// 	c.HTML(http.StatusOK, "index.html", gin.H{
	// 		"title": "Main website",
	// 	})
	// })

	r.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusPermanentRedirect, "/static/index.html")
	})

	r.GET("/index", func(c *gin.Context) {
		c.Redirect(http.StatusPermanentRedirect, "/static/index.html")
	})
	// 404
	r.NoRoute(func(c *gin.Context) {
		c.Redirect(http.StatusPermanentRedirect, "/static/index.html")
	})
	//加载api
	//路由分组contract
	contractRouter := r.Group("/api")
	//contractRouter.Use(controller.ApiInterceptor())
	contractRouter.Use(controller.CorsInterceptor())
	{

		contractRouter.GET("/getLedgerInfo", controller.GetLedgerInfo)
		contractRouter.GET("/getAllBlock", controller.GetAllBlock)
		contractRouter.GET("/getAllBlockPage", controller.GetAllBlockPage)
		contractRouter.GET("/getAllTransaction", controller.GetAllTransaction)
		contractRouter.GET("/getAllTransactionPage", controller.GetAllTransactionPage)
		contractRouter.GET("/getTransactionById", controller.GetTransactionById)
		contractRouter.GET("/getAllNode", controller.GetAllNode)
		contractRouter.GET("/getAllNodePage", controller.GetAllNodePage)
		contractRouter.GET("/getAllEvent", controller.GetAllEvent)
		contractRouter.GET("/getAllEvenetPage", controller.GetAllEvenetPage)
		//contractRouter.GET("/getBlockChartData", controller.GetBlockChartData)
		contractRouter.GET("/getBlockChartData", controller.GetBlockChartDataV2)
		//contractRouter.GET("/getTransactionChartData", controller.GetTransactionChartData)
		contractRouter.GET("/getTransactionChartData", controller.GetTransactionChartDataV2)
		contractRouter.GET("/getAllChaincode", controller.GetAllChaincode)

		//链码调用
		//basic
		contractRouter.POST("/chaincode/basic/createAsset", controller.BasicCreateAsset)
		contractRouter.POST("/chaincode/basic/readAsset", controller.BasicReadAsset)
		contractRouter.POST("/chaincode/basic/transferAsset", controller.BasicTransferAsset)
		//asset_transfer
		contractRouter.POST("/chaincode/asset_transfer/registerUser", controller.AssetRegisterUser)
		contractRouter.POST("/chaincode/asset_transfer/obtainBalance", controller.AssetObtainBalance)
		contractRouter.GET("/chaincode/asset_transfer/balanceOf", controller.AssetBalanceOf)
		contractRouter.GET("/chaincode/asset_transfer/queryBalanceHistory", controller.AssetQueryBalanceHistory)
		contractRouter.POST("/chaincode/asset_transfer/assetEnroll", controller.AssetEnroll)
		contractRouter.POST("/chaincode/asset_transfer/assetTransfer", controller.AssetTransfer)
		contractRouter.GET("/chaincode/asset_transfer/queryAsset", controller.AssetQuery)
		contractRouter.GET("/chaincode/asset_transfer/queryAssetTransaction", controller.AssetQueryTransaction)
		contractRouter.GET("/chaincode/asset_transfer/queryUserAssets", controller.AssetQueryUserAssets)
		//spectrum链码
		contractRouter.GET("/chaincode/spectrum/queryPUList", controller.SpectrumQueryPUList)
		contractRouter.GET("/chaincode/spectrum/querySensorList", controller.SpectrumQuerySensorList)
		contractRouter.GET("/chaincode/spectrum/queryBidderList", controller.SpectrumQueryBidderList)
		contractRouter.GET("/chaincode/spectrum/queryPUSensorBidderList", controller.SpectrumQueryPUSensorBidderList)
		contractRouter.POST("/chaincode/spectrum/createCSCAndSAC", controller.SpectrumCreateCSCAndSAC)
		contractRouter.POST("/chaincode/spectrum/fusion", controller.SpectrumFusion)
		contractRouter.POST("/chaincode/spectrum/commit", controller.SpectrumCommit)
		contractRouter.POST("/chaincode/spectrum/reveal", controller.SpectrumReveal)
		contractRouter.POST("/chaincode/spectrum/reset", controller.SpectrumReset)
		contractRouter.GET("/chaincode/spectrum/getCSCById", controller.SpectrumGetCSCById)
		contractRouter.GET("/chaincode/spectrum/getSACById", controller.SpectrumGetSACById)

		//自动脚本配置
		contractRouter.GET("/auto/changeInterval", timer.AutoChangeInterval)
		contractRouter.GET("/auto/changeStart", timer.AutoChangeStart)

		//登录
		contractRouter.POST("/login", controller.Login)
		contractRouter.POST("/loginOut", controller.LoginOut)
		contractRouter.GET("/token", controller.GetToken)
	}

	//websocket初始化
	r.Any("/ws", func(c *gin.Context) {
		websocket.WebsocketHandler(c)
	})

	//默认8080端口
	r.Run("0.0.0.0:8080")

}
